/**
 * @file CustomTabBar.java
 * @create 下午7:33:02
 * @author CX
 * @description 自定义的tab bar 控件
 */
package cn.edu.hstc.wm.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.edu.hstc.wm.utils.LogUtil;

import cn.edu.hstc.wm.R;

public class CustomTabBar extends LinearLayout {

	/** 上下文 */
	public Context mContext;

	/** 资源管理器 */
	public Resources mResources;

	/** 标签上的文字 */
	public TextView mTabText1, mTabText2;

	/** 指示器 */
	public ImageView mIndicator;

	public CustomTabBar(Context context) {
		super(context);
		LogUtil.v("CustomTabBar", "CustomTabBar(Context context)");
		mContext = context;
		mResources = context.getResources();
		initView();
	}

	public CustomTabBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		LogUtil.v("CustomTabBar",
				"CustomTabBar(Context context, AttributeSet attrs)");
		mContext = context;
		mResources = context.getResources();
		// mResources = getResources();

		initView();
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View rootView = inflater.inflate(R.layout.custom_tab_bar, null);
		mTabText1 = (TextView) rootView.findViewById(R.id.tv_tab_left);
		mTabText2 = (TextView) rootView.findViewById(R.id.tv_tab_right);
		mIndicator = (ImageView) rootView
				.findViewById(R.id.iv_tab_bar_indicator);
		initIndicator();
	}

	/**
	 * 初始化指示器的初始位置
	 */
	private void initIndicator() {
		// 获取指示器宽度
		int picWidth = BitmapFactory.decodeResource(mResources,
				R.drawable.ic_tab_indicator).getWidth();

		// 获取屏幕的像素宽度
		DisplayMetrics dm = new DisplayMetrics();
		dm = mResources.getDisplayMetrics();
		int widthPixels = dm.widthPixels;

		// 计算偏移位置量
		int offset = (int) ((widthPixels / 2 - picWidth) / 2.0);

		// 设置指示器的初始位置
		Matrix matrix = new Matrix();
		mIndicator.setScaleType(ScaleType.MATRIX);
		// matrix.postTranslate(offset, 0); // 平移偏移量的位移
		// matrix.preTranslate(offset, 0);
		matrix.setTranslate(offset, 0);
		mIndicator.setImageMatrix(matrix);
	}

	/**
	 * 设置tab上的文字
	 * 
	 * @param t1
	 *            设置tab1的文字
	 * @param t2
	 *            设置tab2的文字
	 */
	public void setTabText(String t1, String t2) {
		mTabText1.setText(t1);
		mTabText2.setText(t2);
	}

	public void setTabText(int t1, int t2) {
		this.setTabText(mResources.getString(t1), mResources.getString(t2));
	}

	/**
	 * 设置指示器切换位置
	 * 
	 * @param fromXDelta
	 *            开始的位置
	 * @param toXDelta
	 *            目的位置
	 */
	public void moveIndicator(float fromXDelta, float toXDelta) {
		this.moveIndicator(fromXDelta, toXDelta, 300);
	}

	/**
	 * 设置指示器切换位置
	 * 
	 * @param fromXDelta
	 *            开始的位置
	 * @param toXDelta
	 *            目的位置
	 * @param duration
	 *            动画持续时间
	 */
	public void moveIndicator(float fromXDelta, float toXDelta, int duration) {
		Animation animation = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(duration);
		mIndicator.startAnimation(animation);
	}

	public void setIndicator() {
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		System.out.println("onDraw(Canvas canvas)");
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		System.out.println(widthMeasureSpec + "    " + heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
		System.out.println("onLayout");
	}

}
